package test_achieve.h5_2;

import constant.Constant;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import util.tools.BusinessTools;
import util.tools.CommonTools;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by chenkuo on 2017/7/19.
 */
public class Login {
    @Given("^H进入用户登录界面$")
    public void H进入用户登录界面() throws Throwable {
//        System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//        Constant.driverH = new FirefoxDriver();
        String baseUrl = Constant.h5_2LoginURL;

//        System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        //用手机的模式打开M站的页面

        Map<String, String> mobileEmulation = new HashMap<String, String>();
        mobileEmulation.put("deviceName", "iPhone 6");
        Map<String, Object> chromeOptions = new HashMap<String, Object>();
        chromeOptions.put("mobileEmulation", mobileEmulation);
//        chromeOptions.put("profile.default_content_setting_values.notifications", 2);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        Constant.driverH = new ChromeDriver(capabilities);
//        WebDriver.Navigation navigation= Constant.driverH.navigate();
//        Constant.driverH.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);
//        navigation.to(baseUrl);
        Constant.driverH.get(baseUrl);
//================================================================================
//        Constant.driverH = new ChromeDriver();
//        Constant.driverH.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);
//        Constant.driverH.get(baseUrl);
        Set<String> handles = Constant.driverH.getWindowHandles();
        // 获取当前页面句柄 当前是闪送的页面，但是还会弹出一个 “chrome 设置的页面”
        String handle = Constant.driverH.getWindowHandle();
        // 获取所有页面的句柄，并循环判断不是当前的句柄
        for (String temhandle : Constant.driverH.getWindowHandles()) {
            Constant.driverH.switchTo().window(temhandle);
            String title = Constant.driverH.getTitle();
//            System.out.println(" title======= : " + Constant.driverH.getTitle());
            if ("设置".equals(title)) {//如果是chrome设置的页面，就把他关掉，再切换到 “闪送的页面”
                Constant.driverH.close();
                Constant.driverH.switchTo().window(handle);
            }

        }
    }

    @Given("^H选择密码登录$")
    public void H选择密码登录() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[1]/ul/li[2]")).click();
        //截取的图片以文件的形式返回
//        File srcFile = ((TakesScreenshot) Constant.driverH).getScreenshotAs(OutputType.FILE);
//         //使用copyFile()方法保存获取到的截图文件
//        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot.png"));
//        String issUserUUID = Constant.driverH.manage().getCookieNamed("issUserUUID").getValue();
//        System.out.println("选择密码登录issUserUUID:"+issUserUUID);
    }

    @Given("^H点击快捷登录$")
    public void H点击快捷登录() throws Throwable {
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"快捷登录\"]")).click();

    }

    @Given("^H输入用户账户密码:(.+)：(.+)$")
    public void H输入用户账户密码(String name, String pw) throws Throwable {
        BusinessTools.removeLoginNum(name, "wx");
        Thread.sleep(1000);
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/input[1]")).clear();
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/input[1]")).sendKeys(name);
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/input[2]")).clear();
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/input[2]")).sendKeys(pw);
    }

    @Given("^H输入用户账户:(.+)$")
    public void H输入用户账户(String name) throws Throwable {
        BusinessTools.removeLoginNum(name, "wx");
        Thread.sleep(1000);
        Constant.driverH.findElement(By.cssSelector("input.mobile-login.input-center")).clear();
        Constant.driverH.findElement(By.cssSelector("input.mobile-login.input-center")).sendKeys(name);
    }

    @Given("^H点击发送验证码$")
    public void H点击发送验证码() throws Throwable {
        Constant.driverH.findElement(By.cssSelector("input[value='发送验证码']")).click();
    }

    @And("^H输入图形验证码$")
    public void H输入图形验证码() throws Throwable {
        Thread.sleep(1000);
//        System.out.println("=========H输入图形验证码");
        if (CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"填写图形验证码\"]")) {
            Constant.driverH.findElement(By.cssSelector("img.reset-btn")).click();
            Thread.sleep(1000);
            String captcha = BusinessTools.getH5GraphicalCaptcha(Constant.driverH, "_wx_login");
            //通过js使元素可见后，再sendkeys才可以
            String js1 = "document.getElementsByClassName('hidden-input')[0].setAttribute(\"class\",'input');";
            ((JavascriptExecutor) Constant.driverH).executeScript(js1);
            Constant.driverH.findElement(By.className("hidden-input-hook")).sendKeys(captcha);
        }

//        String js2="document.getElementsByClassName('hidden-input-hook')[0].setAttribute(\"value\","+captcha+");";
//        String js2="document.getElementsByClassName('hidden-input-hook')[0].setAttribute(\"value\",'"+captcha+"');";
//        String js2="document.getElementsByClassName('hidden-input-hook')[0].focus();";
//        ((JavascriptExecutor)Constant.driverH).executeScript(js2);

    }

    @And("^H输入快捷登录图形验证码$")
    public void H输入快捷登录图形验证码() throws Throwable {
        Thread.sleep(1000);
//        System.out.println("=========H输入图形验证码");
        if (CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"填写图形验证码\"]")) {
            Constant.driverH.findElement(By.cssSelector("img.reset-btn")).click();
            Thread.sleep(1000);
            Constant.driverH.findElement(By.cssSelector("img.reset-btn")).click();
            String captcha = BusinessTools.getH5GraphicalCaptcha(Constant.driverH, "_wx_smsLogin");
            //通过js使元素可见后，再sendkeys才可以
            String js1 = "document.getElementsByClassName('hidden-input')[0].setAttribute(\"class\",'input');";
            ((JavascriptExecutor) Constant.driverH).executeScript(js1);
            Constant.driverH.findElement(By.className("hidden-input-hook")).sendKeys(captcha);
        }
    }

    @And("^H输入注册图形验证码$")
    public void H输入注册图形验证码() throws Throwable {
        Thread.sleep(1000);
//        System.out.println("=========H输入图形验证码");
        if (CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"填写图形验证码\"]")) {
            Constant.driverH.findElement(By.cssSelector("img.reset-btn")).click();
            String captcha = BusinessTools.getH5GraphicalCaptcha(Constant.driverH, "_wx_register");
            //通过js使元素可见后，再sendkeys才可以
            String js1 = "document.getElementsByClassName('hidden-input')[0].setAttribute(\"class\",'input');";
            ((JavascriptExecutor) Constant.driverH).executeScript(js1);
            Constant.driverH.findElement(By.className("hidden-input-hook")).sendKeys(captcha);
        }
    }

    @And("^H输入短信验证码$")
    public void H输入短信验证码() throws Throwable {
        Thread.sleep(1000);
        System.out.println("=========H输入语音验证码");
        if (CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"输入验证码\"]")
                || CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"注册\"]")
                || CommonTools.checkElementExit(Constant.driverH, "xpath", "//*[text()=\"重置密码\"]")) {
            //通过js使元素可见后，再sendkeys才可以
            String js1 = "document.getElementsByClassName('hidden-input')[0].setAttribute(\"class\",'input');";
            ((JavascriptExecutor) Constant.driverH).executeScript(js1);
            Constant.driverH.findElement(By.className("hidden-input-hook")).sendKeys(Constant.mobileMessage);
        }

    }

    @Given("^H点击登录按钮$")
    public void H点击登录按钮() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/input[3]")).click();
    }

    @And("^H输入注册密码:(.+)$")
    public void H输入注册密码(String pw) throws Throwable {
        Constant.driverH.findElement(By.name("password")).clear();
        Constant.driverH.findElement(By.name("password")).sendKeys(pw);
    }


    @Then("^H登录成功$")
    public void H登录成功() throws Throwable {
        //登录成功后进入下单页面
        Assert.assertTrue(Constant.driverH.findElement(
                By.cssSelector("span.default-mess")).getText().equals("发件信息"));
    }

    @Then("^H弹出快捷登录页面$")
    public void h弹出快捷登录页面() throws Throwable {
        Assert.assertTrue(Constant.driverH.findElement(
                By.cssSelector("p.pop-title")).getText().equals("快捷登录"));
    }

//    public static  void main(String  args[]){
//        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
//        WebDriver driver =new ChromeDriver();
//         final JavascriptExecutor js = (JavascriptExecutor) driver;
//        js.executeScript("window.open('','testwindow','width=400,height=200')");
//        driver.close();
//        driver.switchTo().window("testwindow");
//        js.executeScript("window.moveTo(0,0);");
//        js.executeScript("window.resizeTo(1400,1024);");
//        driver.get("http://m.ss.bingex.com/user/login.html");
//    }

    @Given("^H点击立即注册$")
    public void H点击立即注册() throws Throwable {
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"立即注册\"]")).click();
    }

    @Given("^H点击忘记密码$")
    public void H点击忘记密码() throws Throwable {
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"忘记密码？\"]")).click();
    }

    @And("^H输入注册账户:(.+)$")
    public void H输入注册账户(String tel) throws Throwable {
        if (tel.equals("空格")) {
            tel = " ";
        } else if (tel.equals("空")) {
            tel = "";
        }
        Constant.driverH.findElement(
                By.cssSelector("input[placeholder='请输入手机号码']")).sendKeys(tel);
    }

    @And("^H点击我已阅读并同意$")
    public void H点击我已阅读并同意() throws Throwable {
        Constant.driverH.findElement(By.cssSelector("img[src='images/noChecked.png']")).click();
    }

    @And("^H点击注册$")
    public void H点击注册() throws Throwable {
        Constant.driverH.findElement(By.cssSelector("input[value='注册']")).click();
    }

    @Then("^H验证提示信息:(.+)$")
    public void H验证提示信息(String inf) throws Throwable {
        boolean b = CommonTools.checkElementExit(Constant.driverH, "cssSelector", "div.iss-toast");//是否会弹出验证码界面
        if (b) {
            String toastMessage = Constant.driverH.findElement(By.cssSelector("div.iss-toast")).getText();
            org.testng.Assert.assertEquals(toastMessage, inf);
        }
    }

    @Then("^H验证用户等级:(.+)$")
    public void H验证用户等级(String level) throws Throwable {
        Thread.sleep(1000);
        String l = Constant.driverH.findElement(By.cssSelector("div.m-l-bg")).getText();
        l = l.replaceAll("\\s*|\t|\r|\n", "");
        System.out.println("用户等级：" + l);
        org.testng.Assert.assertEquals(l, level);
    }

}
